<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>

<script>
import { thirdLogin } from "./apis";
import * as types from "./store/mutation-types";
export default {
  data() {
    return {};
  },
  created() {
    // this.getParams();
  },
  methods: {
    getParams() {
      // localStorage.setItem("token", "df8c82b1-eace-4a65-bb47-928fd1e16008");
      // this.$router.push("/code");
      // let uri = location.href;
      // let urlParam = this.modelFunctions.urlParam();
      // if (uri.indexOf("/#/") !== -1) {
      //   this.checkState(urlParam);
      // } else {
      //   let inner = location.href.split("?");
      //   uri = inner[0] + "#/?" + inner[1].substring(0, inner[1].length - 2);
      //   location.href = uri;
      // }
    },
    async checkState(params) {
      let id_secret = await this.api.obtainId({ state: params.state });
      if (id_secret.clientId && id_secret.clientSecret) {
        this.getToken(id_secret, params);
      }
    },
    getToken(data, code_state) {
      this.$store.commit(types.REMOVE_ALL);
      this.loading = true;
      let params = {
        id: data.clientId,
        secret: data.clientSecret,
        code: code_state.code,
        state: code_state.state
      };
      thirdLogin(params)
        .then(async res => {
          localStorage.setItem("token", res.access_token);
          this.$router.push("/code");
        })
        .catch(err => {
          this.log("[登录]", err);
          this.$notify({ type: "danger", message: err.message });
        })
        .finally(() => {
          this.loading = false;
        });
    }
  }
};
</script>

<style lang="less"></style>
